<template>
  <el-container class="box">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <span>vue-element 后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 页面主体 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '5%' : '16%'">
        <div class="hidden-btn">
          <img :src="isCollapse ? imgSrc1 : imgSrc2" alt="" @click="h_btn">
          <!-- <img src="~assets/img/home/hidden.svg" alt=""> -->
        </div>
        <el-menu 
            background-color="#333744" 
            text-color="#fff"
            active-text-color="#ffd04b"
            unique-opened
            :collapse="isCollapse"
            :collapse-transition="false"
            router
            :default-active="activePath">
          <!-- 一级菜单 -->
          <el-submenu :index="'/' + item.path" v-for="item in menulist" :key="item.id">
            <template slot="title">
              <i :class="item.icon"></i>
              <span>{{ item.authName }}</span>
            </template>
            <el-menu-item 
                  :index="'/' + subItem.path" 
                  v-for="subItem in item.children" :key="subItem.id" 
                  @click="itemClick('/' + subItem.path)">
              <template slot="title">
                <i :class="subItem.icon"></i>
                <span >{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>  
      </el-aside>
      <!-- 右侧内容主体 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'Home',
  data() {
    return {
      // 菜单栏数据
      menulist: [],
      isCollapse: false,
      width: 20,
      imgSrc1: require('assets/img/home/fold.svg'),
      imgSrc2: require('assets/img/home/open.svg'),
      activePath: ''
    }
  },
  created() {
    this.getMenuList();
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout() {
      alert('确定退出?');
    },
    async getMenuList() {
      const { data: res } = await this.axios.get('data/menu.json');
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.menulist = res.data
      console.log(res);
    },
    h_btn() {
      this.isCollapse = !this.isCollapse
    },
    // 保存链接的激活状态
    itemClick(activePath) {
      window.sessionStorage.setItem('activePath', activePath);
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
  .box {
    width: 100%;
    height: 100%;
    position: fixed;
  }
  .el-header {
    background-color: #409eff;
    display: flex;
    justify-content: space-between;
    align-items: center;
    > div {
      color: #fff;
      font-size: 18px;
    }
    > .el-button {
      line-height: 10px;
    }
  }
  .el-aside {
    background-color: #333744;
    color: #fff;
    .hidden-btn {
      text-align: center;
      background-color: #4a5064;
      color: #fff;
      line-height: 24px;
      > img {
        margin-top: 5px;
        cursor: pointer;
      }
    }
    .el-menu {
      border: none;
    }
  }
  .el-main {
    background-color: #eaedf1;
  }
</style>